package com.tanhua.dubbo.api;

import com.tan.model.enums.CommentType;
import com.tan.model.mongo.Comment;
import com.tan.model.mongo.Movement;
import com.tan.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer publishComment(String movementId, String comment, Long userId,Integer type){
       Query query = Query.query(Criteria.where("id").is(movementId));
       Movement movement = mongoTemplate.findOne(query, Movement.class);
     Long publishId =  movement.getUserId();
        Comment comment1= new Comment();
        comment1.setUserId(userId);
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setCommentType(type);
        comment1.setContent(comment);
        comment1.setPublishUserId(publishId);
        comment1.setCreated(System.currentTimeMillis());
        mongoTemplate.save(comment1);
        String key =new String();
        if (type== CommentType.COMMENT.getType()){
            key="commentCount";
        }
        if (type== CommentType.LIKE.getType()){
            key="likeCount";
        }
        if (type== CommentType.LOVE.getType()){
            key="loveCount";
        }
        FindAndModifyOptions ooptions = new FindAndModifyOptions();
        ooptions.returnNew(true);//获取更新后的最新消息
      Movement movement1 =  mongoTemplate.findAndModify(query,new Update().inc(key,1),ooptions,Movement.class);//更新并且返回最新消息
        return movement1.statisCount(comment1.getCommentType());
    }

    @Override
    public PageResult getComments(String movementId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(CommentType.COMMENT.getType()));
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> list = mongoTemplate.find(query, Comment.class);
       PageResult pageResult = new PageResult(page,pagesize, list.size(),list);
        return pageResult;
    }

    @Override
    public Boolean getbooleanlike(String moventventid, Long id) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(moventventid)).and("userId").is(id).and("commentType").is(CommentType.LIKE.getType()));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (comment==null){
            return false;
        }
        return true;
    }

    @Override
    public Integer deletelike(String moventid, Long userId, int type) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(moventid)).and("userId").is(userId).and("commentType").is(type));
        mongoTemplate.remove(query,Comment.class);
        String key =new String();
        if (type== CommentType.COMMENT.getType()){
            key="commentCount";
        }
        if (type== CommentType.LIKE.getType()){
            key="likeCount";
        }
        if (type== CommentType.LOVE.getType()){
            key="loveCount";
        }

        FindAndModifyOptions ooptions = new FindAndModifyOptions();
        ooptions.returnNew(true);//获取更新后的最新消息
        Query queryForUpdate = Query.query(Criteria.where("id").is(moventid)); // 重新构建查询条件
        Movement movement1 = mongoTemplate.findAndModify(queryForUpdate, new Update().inc(key, -1), ooptions, Movement.class);
        return movement1.statisCount(type);
    }

    @Override
    public Boolean getbooleanlove(String moventventid, Long id) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(moventventid)).and("userId").is(id).and("commentType").is(CommentType.LOVE.getType()));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (comment==null){
            return false;
        }
        return true;
    }

    @Override
    public Integer savecommentlike(String commenid) {
        Query query = Query.query(Criteria.where("Id").is(new ObjectId(commenid)));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
       Update update = new Update().set("likeCount", comment.getLikeCount()+1);
        mongoTemplate.updateFirst(query,update,Comment.class);
        Query query1 = Query.query(Criteria.where("Id").is(new ObjectId(commenid)));
        Comment comment1 = mongoTemplate.findOne(query, Comment.class);
        return comment1.getLikeCount();
    }

    @Override
    public Integer deletecommentlike(String commentid) {
        Query query = Query.query(Criteria.where("Id").is(new ObjectId(commentid)));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
         Update update = new Update().set("likeCount", comment.getLikeCount()-1);
         mongoTemplate.updateFirst(query,update,Comment.class);
        Query query1 = Query.query(Criteria.where("Id").is(new ObjectId(commentid)));
        Comment comment1 = mongoTemplate.findOne(query, Comment.class);
        return comment1.getLikeCount();
    }

    @Override
    public List<Comment> getLikeCount(Long userId, int type) {
        Query query = Query.query(Criteria.where("publishUserId").is(userId).and("commentType").is(type));
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        return list;
    }

}